package utils

import (
	"regexp"
	"strings"
)

var passwordPatterns = []*regexp.Regexp{
	regexp.MustCompile(`"passwd"\s*:\s*"[^"]*"`),
	regexp.MustCompile(`'passwd'\s*:\s*'[^']*'`),
	regexp.MustCompile(`\\"passwd\\"\s*:\s*\\"[^\\"]*\\"`),
}

// MaskPassword masks password in JSON strings for logging
func MaskPassword(input string) string {
	result := input
	for _, pattern := range passwordPatterns {
		result = pattern.ReplaceAllStringFunc(result, func(match string) string {
			// Find the colon position
			colonIdx := strings.Index(match, ":")
			if colonIdx == -1 {
				return match
			}
			// Keep the field name and replace the value
			return match[:colonIdx+1] + `"***MASKED***"`
		})
	}
	return result
}
